package servlet;

import controller.ControllerAnnunciLocal;
import controller.ControllerIscrizioneAnnuncioLocal;
import entity.Adesione;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.Utilities;

/**
 * Servlet per la gestione del pagamento degli articoli di un iscritto a un annuncio.
 * @author Ale
 */
public class PagamentoServlet extends HttpServlet {

    @EJB
    private ControllerIscrizioneAnnuncioLocal controllerIscrizioneAnnuncio;
    @EJB
    private ControllerAnnunciLocal controllerAnnunci;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if (action == null) {
            Utilities.errore("Nessuna action specificata alla PagamentoServlet", request, response);
        } else if ("mostraPagaAdesione".equals(action)) {
            mostraPagaAdesione(request, response);
        } else if ("pagamentoEseguito".equals(action)) {
            pagamentoEseguito(request, response);
        } else if ("pagamentoAnnullato".equals(action)) {
            pagamentoAnnullato(request, response);
        }

    }

    private void mostraPagaAdesione(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("mostraPagamento", false);

        long idAdesione = Long.parseLong(request.getParameter("idAdesione"));
        Adesione a = controllerAnnunci.getAdesione(idAdesione);
        request.setAttribute("adesione", a);
        request.setAttribute("annuncio", a.getAnnuncio());

        utilities.Utilities.forward("/pagaAdesione.jsp", request, response);
    }

    private void pagamentoEseguito(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long idAdesione = Long.parseLong(request.getParameter("idAdesione"));
        controllerIscrizioneAnnuncio.salvaPagamento(idAdesione);
        utilities.Utilities.forward("/Controller?action=mostraAnnunciIscritto&idAdesione=" + idAdesione, request, response);
    }

    private void pagamentoAnnullato(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        mostraPagaAdesione(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
}
